﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
	/// <summary>
	/// 场景特效配表脚本
	/// </summary>
	[ExecuteInEditMode]
	public class SceneEffect : SceneObject
	{

#if UNITY_EDITOR
		[Header("预览对象资源")]
		[SerializeField]
		private AssetID asset;

		[SerializeField]
		[Tooltip("The offset of the door view.")]
		private Vector3 offset;

		[SerializeField]
		[Tooltip("The rotation of the door view.")]
		private Vector3 rotation;



		public AssetID AssetID
		{
			get { return this.asset; }
			set { this.asset = value; }
		}

		public Vector3 Offset {	get { return this.offset; }}
		public Vector3 Rotation { get { return this.rotation; } }


		private void Awake()
		{
			this.LoadPreview();
		}

		void OnValidate()
		{
			if (Application.isPlaying) return;

			this.name = "Effect";
			this.LoadPreview();
		}
		private void LoadPreview()
		{
			if (!this.asset.IsEmpty)
			{
				var previewPrefab = AssetManager.LoadObjectLocal(
				this.asset, typeof(GameObject)) as GameObject;
				if (previewPrefab)
				{
					var preview = Instantiate(previewPrefab);
					preview.transform.localPosition = this.offset;
					var previewObj = this.GetOrAddComponent<PreviewObject>();
					previewObj.SetPreview(preview);
				}
			}
			else
			{
				var previewObj = this.GetComponent<PreviewObject>();
				if (previewObj != null)
				{
					previewObj.ClearPreview();
				}
			}
		}


#endif

	}
}

